package com.company.management.exception;

import com.company.management.utils.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * 异常处理理类
 */
@ControllerAdvice
@Slf4j
public class CustomExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonData handle(Exception e){
        log.error("[ 系统异常 ]{}",e);
        if( e instanceof XDException ){
            XDException xdException = (XDException) e;
            return
                    JsonData.buildError(xdException.getCode(),xdException.getMsg());
        }else {
            return JsonData.buildError("全局异常，未知错误=>"+e.getMessage());
        }
    }
}